package github.leavesczy.reactivehttpsamples.core.http

import android.util.Log
import com.chuckerteam.chucker.api.ChuckerCollector
import com.chuckerteam.chucker.api.ChuckerInterceptor
import github.leavesczy.reactivehttp.callback.BaseRequestCallback
import github.leavesczy.reactivehttp.datasource.RemoteExtendDataSource
import github.leavesczy.reactivehttp.exception.ReactiveHttpException
import github.leavesczy.reactivehttp.viewmodel.IUIAction
import github.leavesczy.reactivehttpsamples.MainApplication
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.coroutineScope
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.launch
import okhttp3.MediaType
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/**
 * @Author: leavesCZY
 * @Date: 2020/6/23 0:37
 * @Desc:
 * @Github：https://github.com/leavesCZY
 */
class AppRemoteDataSource(iuiAction: IUIAction?) : RemoteExtendDataSource<ApiService>(
    uiAction = iuiAction,
    baseHttpUrl = HttpConfig.BASE_URL_MAP,
    apiServiceClass = ApiService::class.java
) {

    companion object {


        val STREAM: MediaType? = MediaType.parse("application/octet-stream")

        val JSON: MediaType? = MediaType.parse("application/json; charset=utf-8")

        val FORM: MediaType? = MediaType.parse("multipart/form-data")

         val URLEN: MediaType? = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8")


        private val okHttpClient: OkHttpClient by lazy {
            createOkHttpClient()
        }

        private fun createOkHttpClient(): OkHttpClient {
            val builder = OkHttpClient.Builder()
                .readTimeout(120000L, TimeUnit.MILLISECONDS)
                .writeTimeout(120000L, TimeUnit.MILLISECONDS)
                .connectTimeout(600000L, TimeUnit.MILLISECONDS)
                .retryOnConnectionFailure(true)
                //.addInterceptor(FilterInterceptor())
                .addInterceptor(
                    ChuckerInterceptor.Builder(MainApplication.context)
                        .collector(ChuckerCollector(MainApplication.context))
                        .maxContentLength(250000L)
                        .build()
                )
            val loggingInterceptor=HttpLoggingInterceptor(HttpLoggingInterceptor.Logger { message ->
                Log.e("http api->",message)
            })
            loggingInterceptor.level = HttpLoggingInterceptor.Level.BODY
            builder.addInterceptor(loggingInterceptor)
            return builder.build()
        }

    }

    /**
     * 在这里来实现构建 Retrofit 的逻辑
     * @param baseHttpUrl
     */
    override fun createRetrofit(baseHttpUrl: String): Retrofit {
        return Retrofit.Builder()
            .client(okHttpClient)
            .baseUrl(baseHttpUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    override fun generateException(throwable: Throwable): ReactiveHttpException {
        return super.generateException(throwable)
    }

    override fun exceptionHandle(httpException: ReactiveHttpException): Boolean {
        return super.exceptionHandle(httpException)
    }

}